package org.dromara.swimming.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.swimming.domain.bo.SwimmingBo;
import org.dromara.swimming.domain.vo.SwimmingVo;
import org.dromara.swimming.domain.Swimming;
import org.dromara.swimming.mapper.SwimmingMapper;
import org.dromara.swimming.service.ISwimmingService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 景点侦测Service业务层处理
 *
 * @author Ashufen
 * @date 2024-05-07
 */
@RequiredArgsConstructor
@Service
public class SwimmingServiceImpl implements ISwimmingService {

    private final SwimmingMapper baseMapper;

    /**
     * 查询景点侦测
     */
    @Override
    public SwimmingVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询景点侦测列表
     */
    @Override
    public TableDataInfo<SwimmingVo> queryPageList(SwimmingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Swimming> lqw = buildQueryWrapper(bo);
        Page<SwimmingVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询景点侦测列表
     */
    @Override
    public List<SwimmingVo> queryList(SwimmingBo bo) {
        LambdaQueryWrapper<Swimming> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Swimming> buildQueryWrapper(SwimmingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Swimming> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, Swimming::getId, bo.getId());
        lqw.eq(bo.getLatitude() != null, Swimming::getLatitude, bo.getLatitude());
        lqw.eq(bo.getLongitude() != null, Swimming::getLongitude, bo.getLongitude());
        return lqw;
    }

    /**
     * 新增景点侦测
     */
    @Override
    public Boolean insertByBo(SwimmingBo bo) {
        Swimming add = MapstructUtils.convert(bo, Swimming.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改景点侦测
     */
    @Override
    public Boolean updateByBo(SwimmingBo bo) {
        Swimming update = MapstructUtils.convert(bo, Swimming.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Swimming entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除景点侦测
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
